package com.example.mydy.withdraw;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.WithdrawalRecord;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class WithdrawalRecordAdapter extends RecyclerView.Adapter<WithdrawalRecordAdapter.ViewHolder> {

    private Context context;
    private List<WithdrawalRecord> records;
    private SimpleDateFormat dateFormat;

    public WithdrawalRecordAdapter(Context context, List<WithdrawalRecord> records) {
        this.context = context;
        this.records = records;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_withdrawal_record, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WithdrawalRecord record = records.get(position);
        
        // 设置ID
        holder.tvWithdrawalId.setText("WD" + String.format("%09d", record.getId()));
        
        // 设置金额
        holder.tvAmount.setText(String.format("¥ %.2f", record.getAmount()));
        
        // 设置申请时间
        holder.tvApplyTime.setText(dateFormat.format(record.getApplyTime()));
        
        // 设置处理时间（如果有）
        if (record.getProcessTime() != null) {
            holder.layoutProcessTime.setVisibility(View.VISIBLE);
            holder.tvProcessTime.setText(dateFormat.format(record.getProcessTime()));
        } else {
            holder.layoutProcessTime.setVisibility(View.GONE);
        }
        
        // 设置支付宝账号
        String alipayAccount = record.getAlipayAccount();
        if (alipayAccount.length() > 7) {
            String maskedAccount = alipayAccount.substring(0, 3) + "****" + 
                    alipayAccount.substring(alipayAccount.length() - 4);
            holder.tvAlipayAccount.setText(maskedAccount);
        } else {
            holder.tvAlipayAccount.setText(alipayAccount);
        }
        
        // 设置备注（拒绝原因）
        if (!TextUtils.isEmpty(record.getRemarks()) && 
                record.getStatus().equals(WithdrawalRecord.STATUS_REJECTED)) {
            holder.tvRemarks.setVisibility(View.VISIBLE);
            holder.tvRemarks.setText("拒绝原因：" + record.getRemarks());
        } else {
            holder.tvRemarks.setVisibility(View.GONE);
        }
        
        // 设置状态
        switch (record.getStatus()) {
            case WithdrawalRecord.STATUS_PENDING:
                holder.tvStatus.setText("审核中");
                holder.tvStatus.setBackgroundResource(R.drawable.status_pending_bg);
                holder.tvStatus.setTextColor(context.getResources().getColor(android.R.color.holo_orange_dark));
                break;
            case WithdrawalRecord.STATUS_APPROVED:
                holder.tvStatus.setText("已通过");
                holder.tvStatus.setBackgroundResource(R.drawable.status_approved_bg);
                holder.tvStatus.setTextColor(context.getResources().getColor(android.R.color.holo_green_dark));
                break;
            case WithdrawalRecord.STATUS_REJECTED:
                holder.tvStatus.setText("已拒绝");
                holder.tvStatus.setBackgroundResource(R.drawable.status_rejected_bg);
                holder.tvStatus.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
                break;
        }
    }

    @Override
    public int getItemCount() {
        return records.size();
    }

    public void updateData(List<WithdrawalRecord> newRecords) {
        this.records = newRecords;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvWithdrawalId;
        TextView tvStatus;
        TextView tvAmount;
        TextView tvApplyTime;
        TextView tvProcessTime;
        TextView tvAlipayAccount;
        TextView tvRemarks;
        LinearLayout layoutProcessTime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvWithdrawalId = itemView.findViewById(R.id.tv_withdrawal_id);
            tvStatus = itemView.findViewById(R.id.tv_status);
            tvAmount = itemView.findViewById(R.id.tv_amount);
            tvApplyTime = itemView.findViewById(R.id.tv_apply_time);
            tvProcessTime = itemView.findViewById(R.id.tv_process_time);
            tvAlipayAccount = itemView.findViewById(R.id.tv_alipay_account);
            tvRemarks = itemView.findViewById(R.id.tv_remarks);
            layoutProcessTime = itemView.findViewById(R.id.layout_process_time);
        }
    }
} 